//
//  WelcomeViewController.swift
//  TakeTime
//
//  Created by bachelor on 16/11/7.
//  Copyright © 2016年 bachelor. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        if let urlString = sharedUserAccount?.avatar_large {
            iconView.sd_setImage(with: URL(string: urlString))
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // 欢迎归来动画
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        bottomConstraint?.constant = -UIScreen.main.bounds.height + 200
        
        UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5.0, options: UIViewAnimationOptions(rawValue: 0), animations: {
            self.view.layoutIfNeeded()
        }) { _ in
            UIView.animate(withDuration: 1.0, animations: {
                self.messageLabel.alpha = 1.0
            }, completion: { _ in
                NotificationCenter.default.post(name: Notification.Name(rawValue: ZSwitchVCNotification), object: true)
            })
        }
    }

    var bottomConstraint: NSLayoutConstraint?
    // MARK: - 界面设置
    override func loadView() {
        view = UIView()
        
        let backgroundView = UIImageView(image: UIImage(named: "ad_background"))
        view.addSubview(backgroundView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        backgroundView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[subView]|", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: nil, views: ["subView" : backgroundView]))
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[subView]|", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: nil, views: ["subView" : backgroundView]))
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0))
        bottomConstraint = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: -160)
        view.addConstraint(bottomConstraint!)
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[subView(90)]", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: nil, views: ["subView" : iconView]))
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[subView(90)]", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: nil, views: ["subView" : iconView]))
        
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: iconView, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: iconView, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 8))
    }

    lazy var iconView: UIImageView = {
        
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius = 45
        
        return iv
    }()
    
    lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        label.text = "欢迎归来"
        label.sizeToFit()
        
        label.alpha = 0
        
        return label
    }()

}
